package com.ruoyi.aitqjkj.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.aitcommon.utils.*;
import com.ruoyi.aitu9c.utils.U9JSONResultUtils;
import com.sun.jna.WString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 工程客服集成
 */
@Service
public class GCKFService  {

    @Autowired
    IAitEntityInfoService infoService;
    @Autowired
    EntityCache entityCache;

    //获取appToken参数
    private String URL = "http://116.205.182.63";
    private String appToken = null;
    private String accToken=null;
    private String URLName = null;

    private void initParm(Map<String,Object> interParm) {

        this.URL = ObjUtils.toString(interParm.get("url"));
        this.URLName = ObjUtils.toString(interParm.get("URLName"));
    }
    //获取appToken
    private String createappToken(Map<String,Object> interParm) throws Exception{
        //固定获取appToken
         String appId = "u9c";
         String appSecuret = "X6d^r1dfi^Yr$Hl@+";
         String tenantid = "qianjia";
         String accountId = "1320837666454897664";
         String language = "zh_CN";

        JSONObject json = new JSONObject();
        json.put("appId", appId);
        json.put("appSecuret", appSecuret);
        json.put("tenantid", tenantid);
        json.put("accountId", accountId);
        json.put("language", language);
        String ret = RestServiceUtils.post(URL + "/ierp/api/getAppToken.do",json.toJSONString() );
        JSONObject retjson = JSONObject.parseObject(ret);
        if(StringUtils.isBlank(retjson.getJSONObject("data").getString("app_token"))) {
            String msg="获取appToken失败!,错误:"+retjson.getString("message");
            throw new Exception(msg);
        }
        this.appToken = retjson.getJSONObject("data").getString("app_token");
        return this.appToken;
    }

    //获取AccessToken
    private String createAccessToken(Map<String,Object> interParm) throws Exception{

        //固定获取AccessToken
        String user = "15892232964";
        String apptoken = this.createappToken(interParm);
        String tenantid = "qianjia";
        String accountId = "1320837666454897664";
        String logintype = "2";

        JSONObject json = new JSONObject();
        json.put("user", user);
        json.put("apptoken", apptoken);
        json.put("tenantid", tenantid);
        json.put("accountId", accountId);
        json.put("logintype", logintype);
        JSONObject retjson = RestServiceUtils.post(URL + "/ierp/api/login.do",null,json);
        if(StringUtils.isBlank(retjson.getJSONObject("data").getString("access_token"))) {
            String msg="获取AccessToken失败!,错误:"+retjson.getString("message");
            throw new Exception(msg);
        }
        this.accToken = retjson.getJSONObject("data").getString("access_token");
        return this.accToken;
    }
    @ServiceLog("工程客服新增")
    public List<String> formImport(List<Map<String, Object>> datas,
                                   Map<String,Object> interParm,
                                   Map<String,Object> entityInfo) throws Exception {
        List<String> retlist=new ArrayList<String>();
//		processStartOne会强制刷新token,这里也只能再次强刷token
//		this.getToken(interParm);
        this.createAccessToken(interParm);
        for(Map<String,Object> data:datas) {
            try {
                String ret=formImportOne(data,interParm);
                entityInfo.put("oper", "新增");
                infoService.insertAitEntityInfo(entityInfo, data, null);
                retlist.add(ret);
            }catch(Exception ex) {
                ex.printStackTrace();
                String err="错误消息:"+ex.getMessage();
                entityInfo.put("oper", "新增");
                infoService.insertAitEntityInfo(entityInfo, data, ex);
                retlist.add(err);
            }
        }
        return retlist;
    }

    //单条数据保存
    private String formImportOne(Map<String, Object> datas, Map<String,Object> interParm) throws Exception{
        this.initParm(interParm);

        // 调用u9服务
        String Url = URL+interParm.get("URLName");
        Map<String,Object> head=new HashMap<String,Object>();
        head.put("accessToken",this.accToken);
        JSONObject retjson = RestServiceUtils.post(Url,head, new JSONObject(datas));
        //处理工程客户返回
        if(!retjson.getBoolean("success"))
            throw new Exception("接口调用失败!"+retjson.getString("message"));
        return retjson.toJSONString();
    }
}
